package com.wjm.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.wjm.entity.Building;
import com.wjm.entity.Classroom;
import com.wjm.entity.Desk;
import com.wjm.entity.Record;
import com.wjm.entity.StuRecInfo;
import com.wjm.service.interf.InitServiceInterface;
import com.wjm.util.CacheUtil;
import com.wjm.util.JsonUtil;
import com.wjm.util.PropertiesUtil;
import com.wjm.util.StringUtil;

@Service("initService")
public class InitServiceImpl implements InitServiceInterface {

	@Resource(name = "buildingService")
	private BuildingServiceImpl buildingService;

	@Resource(name = "classroomService")
	private ClassroomServiceImpl classroomService;

	@Resource(name = "deskService")
	private DeskServiceImpl deskService;

	@Resource(name = "recordService")
	private RecordServiceImpl recordService;

	@Override
	public void afterPropertiesSet() throws Exception {
		// 缓存资源文件，减少io操作
		CacheUtil.setProperties(PropertiesUtil.loadFile(StringUtil.WEB_CONFIG));

		recInfo();

		roomInfo();
		usecheck();

		System.out.println(JsonUtil.getJson(CacheUtil.getUnuseroom()));
		System.out.println(JsonUtil.getJson(CacheUtil.getUseroom()));

	}

	@Override
	public void roomInfo() throws Exception {

		CacheUtil.setBuildings(new HashMap<Integer, Building>());
		CacheUtil.setDesks(new HashMap<Integer, List<Desk>>());
		CacheUtil.setUnuseroom(new HashMap<Integer, List<Classroom>>());
		CacheUtil.setUseroom(new HashMap<Integer, List<Classroom>>());

		// 缓存所有教学楼
		List<Building> bus = buildingService.queryAll();
		for (Building building : bus) {
			// 缓存教学楼
			CacheUtil.getBuildings().put(building.getBuId(), building);
			// 先将教室全部放在可用的队列中
			List<Classroom> rooms = classroomService.queryByBu(building.getBuId());
			CacheUtil.getUnuseroom().put(building.getBuId(), rooms);
			// 缓存座位
			for (Classroom classroom : rooms) {
				List<Desk> desks = deskService.queryByCr(classroom.getCrId());
				CacheUtil.getDesks().put(classroom.getCrId(), desks);
			}
		}
	}

	@Override
	public void usecheck() throws Exception {
		for (Map.Entry<Integer, List<Desk>> entry : CacheUtil.getDesks().entrySet()) {
			for (Desk desk : entry.getValue()) {
				// 判断座位是否可用
				if (desk.getDeskStatus() == 1 && CacheUtil.getRecords().get(desk.getDeskId()) == null) {
					// 获取所属教学楼id座位缓存的key
					int buId = classroomService.queryBelong(desk.getCrId());
					// 找到不可用队列
					List<Classroom> unroom = CacheUtil.getUnuseroom().get(buId);
					// 遍历，找到该教室
					for (int i = 0; i < unroom.size(); i++) {
						if (unroom.get(i).getCrId() == desk.getCrId()) {
							List<Classroom> usroom = null;
							// 放入可用队列
							if (CacheUtil.getUseroom().get(buId) == null) {
								usroom = new ArrayList<Classroom>();
								// 将可用队列放入缓存中
								CacheUtil.getUseroom().put(buId, usroom);
							} else {
								usroom = CacheUtil.getUseroom().get(buId);
							}
							usroom.add(unroom.get(i));
							System.out.println("useroom get " + JsonUtil.getJson(unroom.get(i)));
							// 不可用队列中移除
							unroom.remove(i);
							break;
						}
					}
					break;
				}
			}
		}
		System.out.println(CacheUtil.getUseroom().size());
	}

	@Override
	@Scheduled(cron="0 0 1 * * ?")
	public void recInfo() throws Exception {
		
		// 清除缓存（每天凌晨1点执行）
		CacheUtil.setRecords(null);
		CacheUtil.setStuRecInfos(null);

		// 初始化
		CacheUtil.setStuRecInfos(new HashMap<String, StuRecInfo>());

		Map<Integer, Record> records = new HashMap<>();
		List<Record> list = recordService.queryActive();
		for (Record record : list) {
			System.out.println(JsonUtil.getJson(record));
			records.put(record.getDeskId(), record);
			StuRecInfo info = new StuRecInfo();
			info.setIsHave(true);
			info.setCancel(recordService.queryCancel(record.getStuId()));
			CacheUtil.getStuRecInfos().put(record.getStuNo(), info);
		}
		CacheUtil.setRecords(records);
	}
}
